package mwa.cs.admin.ctrl;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import mwa.cs.dao.SmMenuDAO;
import mwa.cs.database.Database;
import mwa.cs.model.SmMenu;
import mwa.cs.util.JDate;

/**
 * Servlet implementation class MenuMasterSrvl
 */
public class MenuMasterSrvl extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public MenuMasterSrvl() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/JSON");
		if (request.getParameter("process_type").equals("saveData")) {
			saveData(request, response);
		} else if (request.getParameter("process_type").equals("getMenu")) {
			getMenu(request, response);
		}

	}

	@SuppressWarnings("unchecked")
	public void getMenu(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		Database db = new Database();
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		JSONArray voMenuRvoOuteId = new JSONArray();
		JSONArray voMenuName = new JSONArray();
		JSONArray voMenuId = new JSONArray();
		JSONArray voMenuURL = new JSONArray();
		JSONArray voMenuRvoOutePreviousId = new JSONArray();
		JSONArray voMenuRvoOuteParentId = new JSONArray();

		SmMenuDAO voSmMenuDAO = new SmMenuDAO(db);
		List<Map<String, Object>> voResult = voSmMenuDAO.getAllSmMenu();
		Map<String, Object> voMap;
		for (int i = 0; i < voResult.size(); i++) {
			voMap = voResult.get(i);
			if (voMap.get("MENU_ROUTE_ID") == null) {
				voMenuRvoOuteId.add("");
			} else {
				voMenuRvoOuteId.add(voMap.get("MENU_ROUTE_ID"));
			}
			if (voMap.get("MENU_NAME") == null) {
				voMenuName.add("");
			} else {
				voMenuName.add(voMap.get("MENU_NAME"));
			}
			if (voMap.get("MENU_ID") == null) {
				voMenuId.add("");
			} else {
				voMenuId.add(voMap.get("MENU_ID"));
			}
			if (voMap.get("MENU_URL") == null) {
				voMenuURL.add("");
			} else {
				voMenuURL.add(voMap.get("MENU_URL"));
			}
			if (voMap.get("MENU_ROUTE_PREVIOUS_ID") == null) {
				voMenuRvoOutePreviousId.add("");
			} else {
				voMenuRvoOutePreviousId.add(voMap.get("MENU_ROUTE_PREVIOUS_ID"));
			}
			if (voMap.get("MENU_ROUTE_PARENT_ID") == null) {
				voMenuRvoOuteParentId.add("");
			} else {
				voMenuRvoOuteParentId.add(voMap.get("MENU_ROUTE_PARENT_ID"));
			}

		}
		voObject.put("voMenuRouteId", voMenuRvoOuteId);
		voObject.put("voMenuName", voMenuName);
		voObject.put("voMenuId", voMenuId);
		voObject.put("voMenuURL", voMenuURL);
		voObject.put("voMenuRoutePreviousId", voMenuRvoOutePreviousId);
		voObject.put("voMenuRouteParentId", voMenuRvoOuteParentId);
		voOut.print(voObject.toJSONString());

	}

	@SuppressWarnings("unchecked")
	public void saveData(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		int vnUserId = 0;

		if (request.getSession().getAttribute("user_id") != null) {
			vnUserId = Integer.parseInt(""
					+ request.getSession().getAttribute("user_id"));
		} else {
			vnUserId = 1;
		}
		String paMessage = "";
		Database db = new Database();
		PrintWriter voOut = response.getWriter();
		JSONObject voObject = new JSONObject();
		try {
			db.beginTransaction();
			SmMenuDAO voSmMenuDAO = new SmMenuDAO(db);
			voSmMenuDAO.deleteSmMenu();
			String voMenuId[] = request.getParameterValues("menu_id[]");
			String voMenuName[] = request.getParameterValues("menu_name[]");
			String voMenuURL[] = request.getParameterValues("menu_url[]");
			String voMenuParentId[] = request
					.getParameterValues("menu_parent_id[]");
			String voMenuParent[] = request.getParameterValues("menu_parent[]");
			String voRvoOutePreviousId[] = request
					.getParameterValues("menu_rvoOute_previous_id[]");
			String voRvoOuteId[] = request.getParameterValues("menu_rvoOute_id[]");
			String voRvoOuteParentId[] = request
					.getParameterValues("menu_rvoOute_parent_id[]");
			
			for (int i = 0; i < voMenuId.length; i++) {

				String vaMenuId = voMenuId[i];
				String vaMenuName = voMenuName[i];
				String vaMenuURL = voMenuURL[i];
				String vaMenuParentId = voMenuParentId[i];
				String vaMenuParent = voMenuParent[i];
				String vaMenuRvoOutePreviousId = voRvoOutePreviousId[i];
				String vaMenuRvoOuteId = voRvoOuteId[i];
				String vaMenuRvoOuteParentId = voRvoOuteParentId[i];
				int vnMenuSeq = i + 1;
				int vnInsertUserId = vnUserId;
				String vaInsertDate = JDate.getDate();
				String vaInsertTime = JDate.getTime();
				int vnUpdateUserId = 0;
				String vaUpdateDate = null;
				String vaUpdateTime = null;
				SmMenu voSmMenu = new SmMenu(vaMenuId, vaMenuName, vaMenuURL,
						vaMenuParentId, vaMenuParent, vnMenuSeq,
						vaMenuRvoOutePreviousId, vaMenuRvoOuteId,
						vaMenuRvoOuteParentId, vnInsertUserId, vaInsertDate,
						vaInsertTime, vnUpdateUserId, vaUpdateDate,
						vaUpdateTime);
				voSmMenuDAO.insertSmMenu(voSmMenu);

			}
			paMessage = "success";
			db.commit();
		} catch (Exception e) {
			db.rollback();
			paMessage = "fail";
			e.printStackTrace();
		}
		voObject.put("paMessage", paMessage);
		voOut.print(voObject.toJSONString());
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

}
